package org.mybatis.generator.api.plus;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.io.*;
import java.util.Iterator;
import java.util.List;

/**
 * @author wangfupeng
 */
public class MybatisUtilDistinctMapper {

    public static void writeDistinctMapper(String basePackageName, String aspectPackageName, String targetProject, TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String tableName, String mapperPackage) {
        File file = null;
        OutputStream fileOutputStream = null;
        BufferedWriter bufferedWriter = null;
        try {
            String packageNameFolder = mapperPackage.replaceAll("\\.", "/");
            targetProject = targetProject.replaceAll("\\./", "");
            File folder = new File(targetProject + "/" + packageNameFolder + "/distinct");
            folder.mkdirs();
            file = new File(targetProject + "/" + packageNameFolder + "/distinct/Distinct" + tableName + "Mapper.java");
            //如果文件不存在，则创建文件，如果已存在，则覆盖
            file.createNewFile();
            fileOutputStream = new FileOutputStream(file);
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "utf-8"));
            bufferedWriter.write("package " + mapperPackage + ".distinct;\n");
            bufferedWriter.write("import org.apache.ibatis.annotations.Mapper;\n" +
                    "import org.apache.ibatis.annotations.Select;\n" +
                    "import org.springframework.context.annotation.Lazy;\n" +
                    "\n" +
                    "import java.util.List;\n" +
                    "import java.util.Date;\n" +
                    "\n" +
                    "/*提取字段的不重复值的mapper*/\n" +
                    "@Lazy\n" +
                    "@Mapper\n" +
                    "public interface Distinct" + tableName + "Mapper {\n" +
                    "    \n");
            /*List<IntrospectedColumn> introspectedColumns = introspectedTable.getPrimaryKeyColumns();*/
            List<IntrospectedColumn> introspectedColumns = introspectedTable.getAllColumns();
            Iterator<IntrospectedColumn> iterator = introspectedColumns.iterator();
            String pathString = "";
            boolean isFirstParameter = true;
            while (iterator.hasNext()) {
                IntrospectedColumn introspectedColumn = iterator.next();
                String columnName = introspectedColumn.getJavaProperty();
                String[] remarksArray = introspectedColumn.getRemarks().split(":");
                bufferedWriter.write("" +
                        "    /*提取字段的不重复的" + remarksArray[0] + "*/\n" +
                        "    @Select(\"select distinct(" + introspectedColumn.getActualColumnName() + ") from " + introspectedTable.getFullyQualifiedTableNameAtRuntime() + "\")\n");
                bufferedWriter.write("    public List<");
                FullyQualifiedJavaType fullyQualifiedJavaType = introspectedColumn
                        .getFullyQualifiedJavaType();
                String fullyQualifiedName = fullyQualifiedJavaType.getFullyQualifiedName();
                if (fullyQualifiedJavaType.isPrimitive()) {
                    if ("boolean".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                        bufferedWriter.write("Boolean");
                    } else if ("byte".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                            || "double".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                            || "float".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                            || "int".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                            || "long".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                            || "short".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                    ) {
                        bufferedWriter.write("Integer");
                    } else if ("char".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                        bufferedWriter.write("String");
                    }
                } else {
                    if ("java.lang.Boolean".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                        bufferedWriter.write("Boolean");
                    } else if ("java.lang.Byte".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                            || "java.lang.Double".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                            || "java.lang.Integer".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                            || "java.lang.Long".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                            || "java.lang.Short".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                            || "java.lang.BigDecimal".equals(fullyQualifiedJavaType.getFullyQualifiedName())
                    ) {
                        bufferedWriter.write("Integer");
                    } else if ("java.lang.String".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                        bufferedWriter.write("String");
                    } else if ("java.util.Date".equals(fullyQualifiedJavaType.getFullyQualifiedName())) {
                        bufferedWriter.write("Date");
                    } else {
                        bufferedWriter.write("String");
                    }
                }
                bufferedWriter.write("> getDistinct" + MybatisUtilCommon.getProperCase(columnName) + "();\n" +
                        "\n");
            }
            bufferedWriter.write("    \n" + "}");
            bufferedWriter.write("\n");
            System.out.println("write to file success : " + file.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtilCommon.closeBufferWriterAndFileOutputStream(fileOutputStream, bufferedWriter);
        }
    }

}
